//
//  GeneralAuditDetailForm.swift
//  ANOASTORE
//
//  Created by hudan on 2025/8/21.
//  Copyright © 2025 zwb. All rights reserved.
//

import Foundation

final class GeneralAuditDetailForm: UIViewController {
    
    private lazy var tableView: UITableView = {
        var listView = UITableView(frame: .zero, style: .plain)
        listView.delegate = self
        listView.dataSource = self
        listView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 34, right: 0)
        listView.estimatedRowHeight = 100
        listView.rowHeight = UITableView.automaticDimension
        
        
        listView.separatorStyle = .none
        listView.tableFooterView = UIView()
        listView.backgroundColor = Theme.awModuleBgColor
        if #available(iOS 15.0, *) {
            listView.sectionHeaderTopPadding = 0
        }
        
        listView.register(GeneralSepeCell.self, forCellReuseIdentifier: GeneralSepeCell.identifier)
        listView.register(AuditSecHeader.self, forHeaderFooterViewReuseIdentifier: AuditSecHeader.identifier)
        listView.register(AuditItemCell.self, forCellReuseIdentifier: AuditItemCell.identifier)
        
        return listView
    }()
    
    var dataArray = [AuditFlowContent]()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.title = "详情查看"
        
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
    }

}



extension GeneralAuditDetailForm: UITableViewDataSource, UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataArray.count
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        var one: AuditFlowContent
        
        one = self.dataArray[section]
        
        if one.openx {
            if one.type == "1" {
                return one.mainArr.count
                
            } else if one.type == "2" {
                return one.detailArr.count
                
            } else {
                return 0
            }
        } else {
            return 0
        }
        
    }
    
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
            let section = indexPath.section
            let one = self.dataArray[section]
            
            if one.type == "2" && one.detailArr[indexPath.row].conType == .seperate {
                let cell = tableView.dequeueReusableCell(withIdentifier: GeneralSepeCell.identifier, for: indexPath) as! GeneralSepeCell
                
                return cell
            } else {
                let cell = tableView.dequeueReusableCell(withIdentifier: AuditItemCell.identifier, for: indexPath) as! AuditItemCell
                
                if one.type == "1" {
                    let tempArr = one.mainArr[indexPath.row]
                    cell.titlex = tempArr.pre
                    cell.contentx = tempArr.suf
                    cell.conType = tempArr.conType
                    
                    cell.editable = false
                    
                    if tempArr.must == "true" {
                        cell.mustLab.isHidden = false
                    } else {
                        cell.mustLab.isHidden = true
                    }
                    
                    cell.contentLab.addTextChangeClosure {[indexPath] tev in
                        //todo
                        self.dataArray[indexPath.section].mainArr[indexPath.row].suf = tev.text
                    }
                    
                } else if one.type == "2" {
                    let tempArr = one.detailArr[indexPath.row]
                    cell.titlex = tempArr.pre
                    cell.contentx = tempArr.suf
                    cell.conType = tempArr.conType
                    if tempArr.perm == "E" {
                        cell.editable = true
                    } else {
                        cell.editable = false
                    }
                    cell.mustLab.isHidden = (tempArr.must == "false")
                    
                }
                
                return cell
            }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let model = self.dataArray[section]
        
        if model.type == "11" {
            let footer = tableView.dequeueReusableHeaderFooterView(withIdentifier: GeneralAuditTabFooter.identifier)
            return footer
        }
        
        guard let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: AuditSecHeader.identifier) as? AuditSecHeader else {
            fatalError()
        }
        
        let tap = UITapGestureRecognizer()
        tap.numberOfTapsRequired = 1
        tap.addTap { [unowned self] _ in
            
            self.dataArray[section].openx = !self.dataArray[section].openx
            
            let set = IndexSet(integer:section)
            tableView.reloadSections(set, with:UITableView.RowAnimation.automatic)
        }
        
        headerView.addGestureRecognizer(tap)
        
        if model.openx {
            headerView.arrow.setBackgroundImage(UIImage(named: "af_arrow_down"), for: .normal)
        } else {
            headerView.arrow.setBackgroundImage(UIImage(named: "af_arrow_right"), for: .normal)
        }
  
        headerView.lab.text = model.title
        return headerView
    }
}
